﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.KJCG;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;
using StateGrid.OperationLog;

public partial class AddPM : System.Web.UI.Page
{
    /// <summary>
    /// 科技成果操作对象
    /// </summary>
    private KJCG_DAO kjcgDAO = new KJCG_DAO();
    /// <summary>
    /// 日志操作对象
    /// </summary>
    private OperationLog_DAO operationLogDAO = new OperationLog_DAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {
        
        String message = null;
        //获取分类编码
        String flbm = Request["flbm"];
        //年份
        String nf = Request["nf"];

        User user = (User)Session["login"];
        //登陆超时则退到登录页
        if (null == user)
        {
            Response.Write(" <script> top.location = '" + GlobalResource.LOGIN_PAGE + "' </script> ");
            return;
        }
        //登录人
        this.dlrTextBox.Text = user.name;

        KJCG_FL kjcgFL = null;
        //验证年份
        if (null == nf || nf.Length != 4)
        {
            message = "错误：年份信息有误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        if (null == flbm || "".Equals(flbm))
        {
            message = "错误：分类编码为空";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        else
        {
            kjcgFL = kjcgDAO.GetKJCG_FLByFlbm(flbm);
            if (null == kjcgFL)
            {
                message = "错误：没有找到对应的分类编码";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return;
            }
        }

        //设置返回按钮链接
        this.backHyperLink.NavigateUrl = GlobalKJCG.LISTPM_PAGE + "?catalog=" + flbm;

        KJCG_PM kjcgPM = this.kjcgDAO.getMaxKJCG_PM(nf);
       
        //分类编码
        this.flbmTextBox.Text = kjcgFL.flbm;
        //分类名称
        this.flmcTextBox.Text = kjcgFL.flmc;
        //专业代码
        this.zydmTextBox.Text= kjcgFL.zyfl;
        //成果年份
        this.cbnfTextBox.Text = nf;

        //中文名称
        this.zwmcTextBox.Text = "";
        //学科代码
        this.xkdmTextBox.Text = "";
        //中文主题词
        this.zwztcTextBox.Text = "";
        this.pjfsTextBox.Text = "";//评价方式
        this.pjdwTextBox.Text = "";//评价单位
        this.zwwzTextBox.Text = "";//中文文摘
        //评价日期
        this.pjrqTextBox.Text = "";
        //评价意见
        this.pjyjTextBox.Text = "";
        this.qsrqTextBox.Text = "";//起始日期
        this.zzrqTextBox.Text = "";//终止日期
        this.jxmcTextBox.Text = "";//奖项意见
        this.jldwTextBox.Text = "";//奖励单位
        this.jlndTextBox.Text = "";//奖励年度
        this.sjdjTextBox.Text = "";//授奖等级
        this.cqxsTextBox.Text = "";//产权形式
        this.cqmcTextBox.Text = "";//产权名称
        this.cqbsTextBox.Text = "";//产权标识
        this.dydwTextBox.Text = "";//第一单位
        this.dwsxTextBox.Text = ""; //单位属性
        this.lsgxTextBox.Text = ""; //隶属关系

        this.txdzTextBox.Text = "";//通讯地址
        this.lxdhTextBox.Text = "";//联系电话
        this.czdhTextBox.Text = "";//传真电话
        this.lxrTextBox.Text = "";//联系人
        this.dzxxTextBox.Text = "";//电子邮箱
        this.qtdwTextBox.Text = "";//其他单位
        this.dywcrTextBox.Text = "";//第一完成人
        this.zwzcTextBox.Text = "";//职务职称
        this.szdwTextBox.Text = "";//所在单位
        this.dhTextBox.Text = "";//电话
        this.qtwcrTextBox.Text = "";//其他完成人
        this.tjdwyjTextBox.Text = "";//推荐单位意见


        //上报代码
        this.sbdmTextBox.Text = "";

        String cgdm = null;
        //设置图书文件名
        if (null == kjcgPM)
        {
            cgdm = GetCGDM(null);
        }
        else
        {
            cgdm = GetCGDM(kjcgPM.cgdm);
        }
        this.cgdmTextBox.Text = cgdm;
    }

    /// <summary>
    /// 获取用户填写的科技成果篇名信息
    /// </summary>
    /// <returns></returns>
    private KJCG_PM GetKJCGPM()
    {
        KJCG_PM kjcgPM = new KJCG_PM();
        //成果代码
        kjcgPM.cgdm = this.cgdmTextBox.Text;
        //分类编码
        kjcgPM.flbm = this.flbmTextBox.Text;
        //上报编码
        kjcgPM.sbbm = this.sbdmTextBox.Text;
        //专业代码
        kjcgPM.zydm = this.zydmTextBox.Text.Trim();
        //中文名称
        kjcgPM.zwmc = this.zwmcTextBox.Text.Trim();
        //成果类别
        kjcgPM.cglb = this.cglbDropDownList.SelectedValue;
        //起始日期
        if (!"".Equals(this.qsrqTextBox.Text))
            kjcgPM.qsrq = DateTime.Parse(this.qsrqTextBox.Text);
        //终止日期
        if(!"".Equals(this.zzrqTextBox.Text))
            kjcgPM.zzrq = DateTime.Parse(this.zzrqTextBox.Text);
        //学科代码
        kjcgPM.xkdm = this.xkdmTextBox.Text;
        //国图分类
        kjcgPM.gtfl = this.gtflTextBox.Text;
        //中文主题词
        kjcgPM.zwztc = this.zwztcTextBox.Text;
        //中文文摘
        kjcgPM.zwwz = this.zwwzTextBox.Text;
        kjcgPM.ktly = this.ktlyTextBox.Text;//课题来源
        kjcgPM.pjfs = this.pjfsTextBox.Text;//评价方式
        kjcgPM.pjdw = this.pjdwTextBox.Text;//评价单位
        if(!"".Equals(this.pjrqTextBox.Text))
            kjcgPM.pjrq = DateTime.Parse(this.pjrqTextBox.Text);//评价日期

        kjcgPM.pjyj = this.pjyjTextBox.Text;//评价意见
        kjcgPM.jxmc = this.jxmcTextBox.Text;//奖项名称
        kjcgPM.jldw = this.jldwTextBox.Text;//奖励单位
        kjcgPM.jlnd = this.jlndTextBox.Text;//奖励年度
        kjcgPM.sjdj = this.sjdjTextBox.Text;//授奖等级
        kjcgPM.cqxs = this.cqxsTextBox.Text;//产权形式
        kjcgPM.cqmc = this.cqmcTextBox.Text;//产权名称
        kjcgPM.cqbs = this.cqbsTextBox.Text;//产权标识
        kjcgPM.dydw = this.dydwTextBox.Text;//第一单位
        kjcgPM.dwsx = this.dwsxTextBox.Text;//单位属性
        kjcgPM.lsgx = this.lsgxTextBox.Text;//隶属关系
        kjcgPM.txdz = this.txdzTextBox.Text;//通讯地址
        kjcgPM.yzbm = this.yzbmTextBox.Text;//邮政编码
        kjcgPM.lxdh = this.lxdhTextBox.Text;//联系电话
        kjcgPM.czdh = this.czdhTextBox.Text;//传真电话
        kjcgPM.lxr = this.lxrTextBox.Text;//联系人
        kjcgPM.dzxx = this.dzxxTextBox.Text;//电子邮箱
        kjcgPM.qtdw = this.qtdwTextBox.Text;//其他单位
        kjcgPM.dywcr = this.dywcrTextBox.Text;//第一完成人
        kjcgPM.xb = this.xbDropDownList.SelectedValue;//性别
        kjcgPM.zwmc = this.zwmcTextBox.Text;//职务名称
        kjcgPM.szdw = this.szdwTextBox.Text;//所在单位
        kjcgPM.dh = this.dhTextBox.Text;//电话
        kjcgPM.qtwcr = this.qtwcrTextBox.Text;//其他完成人
        kjcgPM.tjdwyj = this.tjdwyjTextBox.Text;//推荐单位意见
        kjcgPM.gx = this.gxCheckBox.Checked;//共享
        kjcgPM.gxrq = DateTime.Now;//更新日期

        return kjcgPM;
    }

    /// <summary>
    /// 获取下一个成果代码
    /// </summary>
    /// <param name="cgdm"></param>
    /// <returns></returns>
    private String GetCGDM(String cgdm)
    {
        String result = null;
        if (null == cgdm || "".Equals(cgdm))
        {
            result = this.cbnfTextBox.Text + "-0001";
        }
        else
        {
            //获取后四位流水号，并在其基础上加1
            String phString = cgdm.Split('-')[1];

            int ph = 0;

            try
            {
                ph = Int32.Parse(phString);
            }
            catch (Exception)
            {
            }

            //当前篇号+1
            ph += 1;

            phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "000" + phString;
            }
            else if (phString.Length == 2)
            {
                phString = "00" + phString;
            }
            else if (phString.Length == 3)
            {
                phString = "0" + phString;
            }
            else if (phString.Length >= 3)
            {
                String message = "错误：篇名数量已经超过了10000篇";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return null;
            }

            result = this.cbnfTextBox.Text + "-" + phString;
        }
        return result;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        //验证成果代码
        if (!this.ValidateCGDM())
        {
            return;
        }
        //验证日期
        if (!this.ValidateRQ(this.qsrqTextBox.Text,"起始日期")
            ||!this.ValidateRQ(this.zzrqTextBox.Text,"终止日期")
            ||!this.ValidateRQ(this.pjrqTextBox.Text,"评价日期"))
        {
            return;
        }
        //验证中文名称
        if (!this.ValidateZWMC())
        {
            return;
        }
        //查重
        if (!this.ValidateZwmcAndDydw())
        {
            return;
        }
        KJCG_PM kjcgPM = this.GetKJCGPM();
        if (this.kjcgDAO.InsertKJCG_PM(kjcgPM))
        {
            Response.Write("<Script>alert('添加科技成果信息成功')</Script>");
            this.BindData();
        }
        else
        {
            Response.Write("<Script>alert('添加科技成果信息失败')</Script>");
        }
    }

    /// <summary>
    /// 验证成果代码
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateCGDM()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        KJCG_PM kjcgPM = this.kjcgDAO.GetKJCG_PMByCgdm(this.cgdmTextBox.Text);
        if (null != kjcgPM)
        {
            this.errorLabel.Text = "错误：该成果代码已存在";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 查重
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateZwmcAndDydw()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        KJCG_PM kjcgPM = this.kjcgDAO.GetKJCG_PMByZwmcAndDydw(this.zwmcTextBox.Text, this.dydwTextBox.Text);
        if (null != kjcgPM)
        {
            this.errorLabel.Text = "错误：同时具有该中文名称和第一单位的篇名已存在";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证中文名称
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateZWMC()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if(null==this.zwmcTextBox.Text||"".Equals(this.zwmcTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：中文名称不能为空";
            flag = false;
        }
        return flag;
    }

    /// <summary>验证日期</summary>
    private Boolean ValidateRQ(String rq, String msg)
    {
        this.errorLabel.Text = "";

        Boolean flag = true;
        if (rq != "")
        {
            try
            {
                DateTime val = DateTime.Parse(rq);
            }
            catch (Exception)
            {
                this.errorLabel.Text = "错误：" + msg + "格式不正确(yyyy-MM-dd).";
                return false;
            }
        }
        return flag;
    }
   
    //全角转半角
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.BindData();
    }
    protected void gtflTextBox_TextChanged(object sender, EventArgs e)
    {
        this.gtflTextBox.Text = GlobalMethod.Semicolon(this.gtflTextBox.Text.Trim());
    }
    protected void zwztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zwztcTextBox.Text = GlobalMethod.Semicolon(this.zwztcTextBox.Text.Trim());
    }
    protected void dydwTextBox_TextChanged(object sender, EventArgs e)
    {
        this.dydwTextBox.Text = GlobalMethod.Semicolon(this.dydwTextBox.Text.Trim());
    }
    protected void qtdwTextBox_TextChanged(object sender, EventArgs e)
    {
        this.qtdwTextBox.Text = GlobalMethod.Semicolon(this.qtdwTextBox.Text.Trim());
    }
    protected void qtwcrTextBox_TextChanged(object sender, EventArgs e)
    {
        this.qtwcrTextBox.Text = GlobalMethod.Semicolon(this.qtwcrTextBox.Text.Trim());
    }
}